<?php
// +----------------------------------------------------------------------
// | Wendasns [ 开源问答社区系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020~2021 http://wendasns.com All rights reserved.
// +----------------------------------------------------------------------
namespace app\wendasns\admin;

use app\admin\AdminBaseController;
use app\wendasns\model\Category as byCategory;

class Category extends AdminBaseController
{
	
	public function index()
	{
		$type = $this->request->filter('question')->get('type','question','isEmpty');
		return view('index',['type'=>$type]);
    }

	public function list()
	{
		$type = $this->request->filter('question')->get('type','question','isEmpty');
		
		$data = byCategory::where('type',$type)->select();
		success('ok', $data->toArray(), ['count'=>count($data)]);
    }
    //添加
    public function dorun()
    {
    	$id = input('post.id',0,'intval');
    	$type = input('post.type','');
    	$name = input('post.name','');
		$dir_name = input('post.dir_name','');
		$icon = input('post.icon','');
		$status = input('post.status',1,'intval');
		
		/*$dm = byCategory::where('id','<>',$id)->whereOr([
			['dir_name','=',$dir_name],
			['name','=',$name]
		])->find();*/
		$dm = byCategory::whereOr('dir_name',$dir_name)->whereOr('name',$name)->find();
		if($dm && $dm->id<>$id){
			error('已有相同分类');
		}
		$data = [
			'type' => $type,
			'name' => $name,
			'dir_name' => $dir_name,
			'icon' => $icon,
			'status' => $status
		];
    	if($id){
    		$data['id'] = $id;
    		byCategory::update($data);
    	}else{
    		byCategory::create($data);
    	}
    	success('操作成功');
    }
    
    //编辑
    public function edit()
	{
		$id = input('post.id',0,'intval');
		$post = input('post.');
		byCategory::update($post);
		success('操作成功');
    }
    //删除
    public function remove()
	{
		$id = input('post.id',[]);
		byCategory::destroy($id);
		
		success('操作成功');
    }
}